<?php

namespace app\index\controller;

use app\common\service\WxPayService;
use library\Controller;
use think\Db;
use think\facade\Config;

/**
 * 社保账单
 * Class Insbill
 * @package app\index\controller
 */
class Insbill extends Base
{
    /**
     * 账单列表
     */
    public function index()
    {
    	$where[] = ['company_id','eq',$this->company['id']];
		$where[] = ['is_deleted','eq',0];
		$where[] = ['status','in',[2,3,4]];
		$stime = $this->request->get('stime');
        if ($stime){
            $where[] = ['account_period','>=',$stime];
        }
		$etime = $this->request->get('etime');
        if ($etime){
            $where[] = ['account_period','<=',$etime];
        }
        $status = $this->request->get('status');
        if($status){
        	if($status == 1){
        		$where[] = ['status','=',2];
        	}
            if($status == 2){
        		$where[] = ['status','=',3];
        	}
        	if($status == 3){
        		$where[] = ['status','=',4];
        	}
        }
		$list = Db::name('system_insbill')->where($where)
				->field('id,code,account_period,annex_name,create_at,status,receivable,passage_fee,receivable_online')
    			->order('id desc')
    			->paginate(10,false,['query'=>request()->param(),'type' => 'page\Page','var_page'  => 'page']);
		$this ->list = $list; 
		Db::name('system_insbill')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>2])->update(['source'=>1]);
		//待付款
        $this ->to_pay = Db::name('system_insbill')->where(['company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>2])->count(); 
    	$this->fetch();
    }


	 /**
     * 下载
     */
    public function download()
    {
        $id = $this->request->get('id');
    	$info = Db::name('system_insbill')->where(['id'=>$id,'company_id'=>$this->company['id'],'is_deleted'=>0])->find();
		if(!$info){
			$this->redirect('index/publicot/transition', ['info'=>'暂无社保账单','type'=>'4']);
		}			
		if($info['annex_url']){
			file_download($info['annex_name'],$info['annex_url']);
		}else{
			$this->error('暂无账单，无法下载');
		}		
    }
    
    
    /**
     * 付款
     */
    public function payment()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
    		$insbill = Db::name('system_insbill')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[2,3]])->find();
    		if(!$insbill){
				$this->error('账单信息有误，无法付款');
			}
			Db::name('system_insbill')->where(['id'=>$data['id']])->update(['status'=>3]);
			//更新财务账单
        	Db::name('system_financial_bill')->where(['bill_type'=>1,'jump_id'=>$data['id']])->update(['status'=>2]);
        	$this->success('付款中......', url('index/insbill/paymentSuccess'));
    	}else{
    		$id = $this->request->get('id');
			$insbill = Db::name('system_insbill')->where(['id'=>$id,'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[2,3]])->find();
			if(!$insbill){
				$this->redirect('index/publicot/transition', ['info'=>'暂无账单信息','type'=>'4']);
			}
			$this ->info = $insbill;
    		$this->fetch();
    	}   	
    }
    
    
    /**
     * 生成微信付款码
     */
    public function payment_code()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
			$insbill = Db::name('system_insbill')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>[2,3]])->find();
    		if(!$insbill){
				$this->error('账单信息有误，无法付款');
			}
			$wxpay = new WxPayService();
			$body = $insbill['account_period'].'账单';
			$platform = Config::get()['wechat']['platform'];
			$pay_code = $insbill['code'].'_native';
			$result = $wxpay->pay($pay_code, $insbill['receivable_online'], 1,$body,$platform);
			if(!$result){
				$this->error('支付二维码获取失败，请稍后再试');
			}
			if(!empty($result['code']) && $result['code'] == 0){
				$this->error('支付二维码获取失败，原因：'.$result['data']);
			}
			$this->success('获取成功',['pay_code'=>$result]);
    	}
    }
    
	
	/**
     * 查询付款信息
     */
    public function query_order()
    {
    	if($this->request->isPost()){
    		$data = $this->request->post();
    		if(empty($data['id']) || !$data['id']){
				$this->error('参数错误');
			}
			$insbill = Db::name('system_insbill')->where(['id'=>$data['id'],'company_id'=>$this->company['id'],'is_deleted'=>0,'status'=>4,'payment_method'=>3])->find();
    		if(!$insbill){
				$this->error('账单未付款');
			}else{
				$this->success('付款成功');
			}
    	}
    }
	
	
	/**
     * 付款成功
     */
    public function paymentSuccess()
    {
        $this->fetch();
    }
	
}
